package jwebtester.security.hash;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

public class Sha512HashFunction implements HashFunction {

	@Override
	public String calculateHash(final String plainPassword) {
		if (StringUtils.isBlank(plainPassword)) {
			throw new IllegalArgumentException("Illegal empty or blank password");
		}

		try {
			final MessageDigest digest = MessageDigest.getInstance("SHA-512");

			final byte[] passwordBytes = plainPassword.getBytes(Charset.defaultCharset());
			final byte[] calculatedHash = digest.digest(passwordBytes);
			return Base64.encodeBase64String(calculatedHash);
		} catch (NoSuchAlgorithmException e) {
			throw new UnsupportedOperationException("Can't instantiate hash algorithm", e);
		}
	}
}
